/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */
#pragma once

#include <Config.h>
#include "Envelope.h"
#include "PrimitiveAnimationBase.h"
#include <properties/DependencyProperty.h>

namespace liba {
namespace animate {

template< typename K >
class PrimitiveAnimation : public PrimitiveAnimationBase
{
public:
	PrimitiveAnimation()
	:	active_envelope(CUBIC)
	{}

	time_float get_length()const
	{
		return active_envelope == CUBIC ? envelope.get_length() : envelope_tcb.get_length();
	}
	void change_position( time_float position )
	{
		properties::BaseDependencyProperty<K> *prty = 0;
		if( target && ( prty = target->FindProperty<K>(param_name) ) )
		{
			prty->Set( active_envelope == CUBIC ? envelope.evaluate( (float)position ) : envelope_tcb.evaluate( (float)position ) );
		}
	}
protected:
	virtual bool on_node(xml::Provider * prov, const std::string & name )
	{
		if( name == "Envelope" )
		{
			active_envelope = CUBIC;
			return prov->parse_object( &envelope );
		}

		if( name == "EnvelopeTCB" )
		{
			active_envelope = TCB;
			return prov->parse_object( &envelope_tcb );
		}

		return false;
	}
private:
	Envelope< K > envelope;
	EnvelopeTCB<K> envelope_tcb;
	enum ActiveEnvelope {CUBIC, TCB};
	ActiveEnvelope active_envelope;
};

} // namespace animate
} // namespace liba
using namespace liba;

